<template>
  <div class="app-container">
    <!-- 条件搜索 -->
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="用户名" prop="userName">
        <el-input v-model="queryParams.userName" placeholder="请输入用户名" clearable size="small"
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="手机号" prop="phoneNumber">
        <el-input v-model="queryParams.phoneNumber" placeholder="请输入手机号" clearable size="small"
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="体验官状态" prop="hasQualification" label-width="100px">
        <el-select v-model="queryParams.hasQualification" clearable placeholder="请选择">
          <el-option label="未申请" :value="0"></el-option>
          <el-option label="审核中" :value="1"></el-option>
          <el-option label="已通过" :value="2"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <!-- 页面操作按钮 -->
    <!-- <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['view:AppUser:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['view:AppUser:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['view:AppUser:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['view:AppUser:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row> -->

    <!-- table表格 -->
    <el-table v-loading="loading" :data="AppUserList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="用户名" align="center" prop="userName" />
      <el-table-column label="手机号" align="center" prop="phoneNumber" width="110">
        <template slot-scope="scope">
          {{scope.row.phoneNumber || '暂未绑定手机号'}}
        </template>
      </el-table-column>
      <!-- <el-table-column label="邮箱地址" align="center" prop="email" /> -->
      <el-table-column label="头像地址" align="center" prop="avatar">
        <template slot-scope="scope">
          <el-image :src="scope.row.avatar" style="width:50px;height:50px" :preview-src-list="[scope.row.avatar]">
          </el-image>
        </template>
      </el-table-column>
      <el-table-column label="乡镇管理员" align="center" prop="townshipAdministrators">
        <template slot-scope="scope">
          {{ scope.row.townshipAdministrators === 'false' ? '否' : '是' }}
        </template>
      </el-table-column>
      <el-table-column label="用户身份" align="center" prop="scanPower">
        <template slot-scope="scope">
          {{ scope.row.scanPower === 1 ? '管理员' : scope.row.scanPower === 2 ? '商家' : scope.row.scanPower === 3 ? '普通用户':'状态错误' }}
        </template>
      </el-table-column>
      <!-- <el-table-column label="备注" align="center" prop="remark" /> -->
      <el-table-column label="注册时间" align="center" prop="registerDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.registerDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="体验官信息" align="center" prop="registerDate" width="180">
        <template slot-scope="scope">
          <el-tag type="info" v-if="scope.row.hasQualification==0">未申请</el-tag>
          <el-tooltip v-else class="item" effect="dark" placement="top">
            <div slot="content">
              <p>姓名：{{scope.row.realName}}</p>
              <p>手机号：{{scope.row.phoneNumber}}</p>
              <p>团队或院校：{{scope.row.team}}</p>
              <p>专业：{{scope.row.speciality}}</p>
            </div>
            <el-tag type="warning" v-if="scope.row.hasQualification==1">审核中 <i class="el-icon-view"></i></el-tag>
            <el-tag type="success" v-if="scope.row.hasQualification==2">已注册 <i class="el-icon-view"></i></el-tag>
          </el-tooltip>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['view:AppUser:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-check" @click="handleExamine(scope.row)"
            v-hasPermi="['view:AppUser:edit']" v-if="scope.row.hasQualification==1">审核</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['view:AppUser:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改微信小程序用户对话框 -->
    <el-dialog :close-on-click-modal="false" :title="title" :visible.sync="open" width="1000px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="用户名" prop="userName">
          <el-input v-model="form.userName" placeholder="请输入用户名" />
        </el-form-item>
        <el-form-item label="手机号" prop="phoneNumber">
          <el-input v-model="form.phoneNumber" disabled placeholder="请输入手机号" />
        </el-form-item>
        <el-form-item label="头像" prop="avatar">
          <imageUpload v-model="form.avatar" @uploadImage="uploadimg" @remove="removeImg" :limit="1" />
        </el-form-item>
        <el-form-item label="乡镇管理员" prop="townshipAdministrators">
          <el-radio-group v-model="form.townshipAdministrators">
            <el-radio label="true" value="true">是</el-radio>
            <el-radio label="false" value="false">否</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="用户身份" prop="scanPower">
          <el-radio-group v-model="form.scanPower">
            <el-radio :label="1" :value="1">管理员</el-radio>
            <el-radio :label="2" :value="2">商家</el-radio>
            <el-radio :label="3" :value="3">普通用户</el-radio>
          </el-radio-group>
        </el-form-item>
        <!-- <el-form-item label="邮箱" prop="email">
          <el-input v-model="form.email" placeholder="请输入邮箱地址" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 添加或修改微信小程序用户对话框 -->
    <el-dialog :close-on-click-modal="false" :title="title" :visible.sync="open2" width="500px" append-to-body>
      <el-form label-width="100px">
        <el-form-item label="姓名">{{form.realName}}</el-form-item>
        <el-form-item label="手机号">{{form.phoneNumber}}</el-form-item>
        <el-form-item label="团队或院校">{{form.team}}</el-form-item>
        <el-form-item label="专业">{{form.speciality}}</el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitExamine(2)">通过</el-button>
        <el-button type="danger" @click="submitExamine(0)">不通过</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import { listAppUser, getAppUser, delAppUser, addAppUser, updateAppUser, exportAppUser } from "@/api/view/AppUser";
  import ImageUpload from "@/components/UploadImage";
  export default {
    name: "AppUser",
    components: {
      ImageUpload
    },
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // app小程序用户表格数据
        AppUserList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        open2: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          userName: null,
          password: null,
          nickName: null,
          phoneNumber: null,
          email: null,
          avatar: null,
          wxOpenid: null,
          registerDate: null
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          userName: [
            { required: true, message: '请输入', trigger: 'blur' },
          ],
          avatar: [
            { required: true, message: '请选择', trigger: 'change' },
          ],
          townshipAdministrators: [
            { required: true, message: '请选择', trigger: 'change' },
          ],
          scanPower: [
            { required: true, message: '请选择', trigger: 'change' },
          ],
        }
      };
    },
    created() {
      this.getList();
    },
    methods: {
      /** 查询app小程序用户列表 */
      getList() {
        this.loading = true;
        listAppUser(this.queryParams).then(response => {
          response.rows.forEach(item => {
            if (item.avatar) {
              if (item.avatar.indexOf('http') === -1) {
                item.avatar = this.baseurl + item.avatar;
              };
            };
          });
          this.AppUserList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.open2 = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: null,
          userName: null,
          password: null,
          nickName: null,
          phoneNumber: null,
          // email: null,
          avatar: null,
          wxOpenid: null,
          // remark: null,
          registerDate: null,
          scanPower: null,
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加微信小程序用户";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const id = row.id || this.ids
        getAppUser(id).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改微信小程序用户";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            updateAppUser(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          }
        });
      },
      handleExamine(row) {
        this.reset();
        const id = row.id || this.ids
        getAppUser(id).then(response => {
          this.form = response.data;
          this.open2 = true;
          this.title = "审核体验官申请";
        });
      },
      submitExamine(type) {
        this.form.hasQualification = type;
        updateAppUser(this.form).then(response => {
          this.msgSuccess("审核成功");
          this.open2 = false;
          this.getList();
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$confirm('是否确认删除用户名称为" ' + row.userName + ' "的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delAppUser(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
      },
      /** 导出按钮操作 */
      handleExport() {
        const queryParams = this.queryParams;
        this.$confirm('是否确认导出所有app小程序用户数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportAppUser(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
      },
      /* 上传图片组件事件 */
      uploadimg(e) {
        this.form.avatar = e;
      },
      removeImg(e) {
        this.form.avatar = e;
      },
    }
  };
</script>